package com.wyman.factory.support;

import com.wyman.BeanException;
import com.wyman.factory.BeanFactory;
import com.wyman.factory.config.BeanDefinition;

/**
 * @Author 李伟民
 * @Description DateTime 2024/11/25 01:43
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    //模板设计模式 定义容器中获取bean的逻辑 但不负责逻辑中具体方法实现
    @Override
    public Object getBean(String beanName) throws BeanException {
        Object bean = getSingleton(beanName);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition,null);
    }

    //获取bean时传递参数 是入口
    @Override
    public Object getBean(String beanName, Object[] args) throws BeanException {
        Object bean = getSingleton(beanName);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition,args);

    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeanException;

    //负责实现 真正创建bean
    protected abstract Object createBean(String beanName,BeanDefinition beanDefinition,Object[] args) throws BeanException;
}
